const APPLY_ORDER_KEY = 'apply_order_cache'

function getSaveName(key) {
  return APPLY_ORDER_KEY + key
}

function paramsConfig() {
  return {
    "type": {
      required: true,
      name: '车辆状态',
      type: 'int'
    },
    "business": {
      required: true,
      name: '业务类型',
      type: 'int'
    },
    "price": {
      required: true,
      name: '佣金',
      type: 'int'
    },
    "clue": {
      required: false,
      name: '线索',
      type: 'string'
    },
    "remark": {
      required: false,
      name: '备注',
      type: 'string'
    },
    "limitPrice": {
      required: false,
      name: '限时佣金',
      type: 'int'
    },
    "limitTime": {
      required: false,
      name: '限时时间',
      type: 'string'
    },
    "car": {
      required: true,
      name: '汽车信息',
      type: 'object'
    },
    "clueImageLst": {
      required: false,
      name: '线索图片',
      type: 'array'
    },
    "location": {
      required: false,
      name: '所在地',
      type: 'string',
      empty_notice:"请选择所在地"
    },
    "locationCoords": {
      required: false,
      name: '所在地坐标',
      type: 'string',
      empty_notice: "请选择所在地"
    },
    "destination": {
      required: true,
      name: '目的地',
      type: 'string',
      empty_notice: "请选择目的地"
    },
    "destinationCoords": {
      required: true,
      name: '所在地',
      type: 'string',
      empty_notice: "请选择目的地"
    },
  }
}

function getAll() {
  let config = paramsConfig()
  let obj = {}
  for (var key in config) {
    obj[key] = formatValue(config[key].type,getItem(key))
  }
  return obj
}

function clearCache() {
  wx.removeStorageSync(APPLY_ORDER_KEY)
  console.log("order cache cleared")
}

function checkValidate(key, value) {

  let config = paramsConfig()

  if (!config.hasOwnProperty(key)) {
    console.log("this key not found")
    return true
  }

  let emptyRequirement = config[key].required
  if (!emptyRequirement)
    return true

  if (value === undefined || value === 'undefined' || value === null || value === '')
    return false
  else
    return true
}

function formatValue(type,value) {
  switch (type) {
    case 'array':
      break;
    case 'object':
      break;
    case 'string':
      value = value ? value + "" : ""
      break;
    case 'int':
      value = parseInt(value)
      break;
  }
  return value
}

function setItem(key, value, isForce) {
  
  var config = paramsConfig()
 
  if (!config.hasOwnProperty(key)) {
    throw new Error("您设置了不存在的key:" + key)
    return
  }

  let name = config[key].name
  let empty_notice = config[key].empty_notice

  if (!isForce && !checkValidate(key, value)) {
    let msg = empty_notice ? empty_notice : name + "需要填写"
    throw new Error(msg)
    return
  }
  
  value = formatValue(config[key].type,value)

  let res = wx.getStorageSync(APPLY_ORDER_KEY)
  console.log(res)
  if (res && typeof res === 'object') {
    res[key] = value
    wx.setStorageSync(APPLY_ORDER_KEY, res)
  } else {
    wx.setStorageSync(APPLY_ORDER_KEY, {
      key: value
    })
  }
  console.log("set " + key + " = ", getItem(key))
}

function getItem(key) {
  let data = wx.getStorageSync(APPLY_ORDER_KEY)
  if (!data)
    return null

  return data[key] === undefined ? null : data[key];

}

function errorNotice(msg) {
  wx.showToast({
    title: msg,
    icon:'none'
  })
}

const UPDATE_CACHE_KEY = 'quest_order_update_key'

function setUpdateInfo (data) {
  wx.setStorageSync(UPDATE_CACHE_KEY, data)
}

function getUpdateInfo() {
  return wx.getStorageSync(UPDATE_CACHE_KEY)
}

function deleteUpdateInfo() {
  wx.removeStorage({
    key: UPDATE_CACHE_KEY,
    success: function(res) {},
  })
}

module.exports = {
  setItem: setItem,
  getItem: getItem,
  clearCache: clearCache,
  getAll: getAll,
  setUpdateInfo: setUpdateInfo,
  getUpdateInfo: getUpdateInfo,
  deleteUpdateInfo: deleteUpdateInfo
}